/**
 * Created by 功振 on 2016/2/2.
 * 测试dockerfile
 */
angular.module('NatureCloud').controller('testDockerController', ["$scope", "$cookies","$state", "$stateParams", "$mdDialog","$interval","$timeout","$document","dockerTest","buildLastQuery","dockerShow","buildStart", "projectCodeInfoService", "builderService","params",
    function ($scope, $cookies,$state,$stateParams,$mdDialog,$interval,$timeout,$document,dockerTest,buildLastQuery,dockerShow, buildStart, projectCodeInfoService,builderService,params) {
        var user = $cookies.get("username");
        $scope.buildInfo=params;
        $scope.sub = {};
        $scope.sub.version = "master";
        $scope.type = params.type;
        var interval;
        $scope.editorOptions = {
            lineWrapping : false,
            autoRefresh:true,
            lineNumbers: true,
            theme:'monokai',
            model: 'yaml'
        };
        $scope.dockerfile ='';
        var tem=function () {
            var user=$cookies.get("username");
            dockerShow(user,params, $scope.sub.version,$scope.sub.branchtype).then(function (r) {
                $scope.dockerfile =r ;
            });
        };
        tem();
        $scope.dockertest ='waiting...';
        $scope.codemirrorLoaded =function(editorOptions){
            var scroll=editorOptions.getScrollInfo();
            editorOptions.on("beforeChange", function() {
            })
            editorOptions.on("change", function(){
                var i=editorOptions.lastLine();
                editorOptions.scrollTo(0,500*(i/20));
            });
        }


        var runID = params.id;
        $scope.jobName = runID;
        buildLastQuery(user, runID).then(function (r) {
            var runInfo = r;
            var tagname= r.actions[0].parameters[0].value;
            var version;
            if(tagname){
                version=tagname;
            }else{
                version='master';
            }
            var test = function () {
                dockerTest(user, params,  version, $scope.sub.branchtype).then(function (r) {
                });
            };
            test();
            var getBuild = function (runID, runInfo) {
                interval = $interval(function () {
                    builderService.getBuildConOut(runID, runInfo.number + 1,0).then(function (response) {
                        //alert(response.data);
                        var str='';
                        for(var i=0;i<response.data.length;i++){
                            str+=response.data[i]+'\n';
                        }
                        $scope.dockertest = str;
                        //var str = JSON.stringify(response.data);

                        if(str.indexOf('Finished: SUCCESS') >= 1 || str.indexOf('Finished: FAILURE') >= 1){
                            $scope.isHide = true;
                            $interval.cancel(interval);
                        }
                    });
                }, 3000);

            }

            getBuild(runID, runInfo);

        });
        $scope.$on("$destroy", function() {
            if (interval) {
                $interval.cancel(interval);
            }
        });
        $scope.cancel=function(){
            $mdDialog.cancel();
        };
    }]);